<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">
        <i class="el-icon-s-promotion"/> 首页
      </el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>新增SPU</el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider/>
    <el-steps :active="1" align-center finish-status="success">
      <el-step title="步骤1" description="选择商品类别"></el-step>
      <el-step title="步骤2" description="填写商品基本信息"></el-step>
      <el-step title="步骤3" description="选择商品相册"></el-step>
    </el-steps>
    <el-divider/>
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="130px" class="demo-ruleForm">
      <el-form-item label="品牌" prop="brandId">
        <el-select v-model="ruleForm.brandId" placeholder="请选择">
          <el-option
              v-for="item in brandListOptions"
              :key="item.id"
              :label="item.name"
              :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="商品名称" prop="name">
        <el-input v-model="ruleForm.name"></el-input>
      </el-form-item>
      <el-form-item label="标题" prop="title">
        <el-input v-model="ruleForm.title"></el-input>
      </el-form-item>
      <el-form-item label="简介" prop="description">
        <el-input v-model="ruleForm.description"></el-input>
      </el-form-item>
      <el-form-item label="货号" prop="typeNumber">
        <el-input v-model="ruleForm.typeNumber"></el-input>
      </el-form-item>
      <el-row>
        <el-col :span="12">
          <el-form-item label="计件单位" prop="unit">
            <el-input v-model="ruleForm.unit"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="售价" prop="listPrice">
            <el-input v-model="ruleForm.listPrice"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="库存" prop="stock">
            <el-input v-model="ruleForm.stock"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="库存阈警值" prop="stockThreshold">
            <el-input v-model="ruleForm.stockThreshold"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="关键词" prop="keywords">
        <el-input v-model="ruleForm.keywords"></el-input>
      </el-form-item>
      <el-form-item label="标签" prop="tags">
        <el-input v-model="ruleForm.tags"></el-input>
      </el-form-item>
      <el-form-item label="排序序号" prop="sort">
        <el-input v-model="ruleForm.sort"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="warning" @click="backToPreStep()">上一步</el-button>
        <el-button type="primary" @click="submitForm('ruleForm')">下一步</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      brandListOptions: [
        {id: 1, name: '手机'},
        {id: 2, name: '笔记本'},
        {id: 3, name: '数码'},
      ],
      ruleForm: {
        categoryId: '',
        categoryName: '',
        brandId: '',
        brandName: '',
        name: 'Redmi Note 11 5G',
        title: 'Redmi Note 11 5G 天玑810 33W Pro快充 5000mAh大电池 6GB +128GB 浅梦星河',
        description: '【品质好物】天玑810，5000mAh大电池【Note11SE火热抢购中】',
        typeNumber: '100028235472',
        unit: '部',
        listPrice: '1199',
        stock: '5000',
        stockThreshold: '500',
        keywords: '小米,红米,智能手机',
        tags: '5G手机,快充手机,大容量电池手机',
        sort: '95'
      },
      // 验证规则
      rules: {
        brandId: [
          {type: 'number', required: true, message: '请选择品牌', trigger: 'blur'},
        ],
        name: [
          {required: true, message: '请输入商品名称', trigger: 'blur'},
        ],
        title: [
          {required: true, message: '请输入标题', trigger: 'blur'},
        ],
        description: [
          {required: true, message: '请输入简介', trigger: 'blur'},
        ],
        unit: [
          {required: true, message: '请输入计件单位', trigger: 'blur'},
        ],
        listPrice: [
          {required: true, message: '请输入售价', trigger: 'blur'},
        ],
        typeNumber: [
          {required: true, message: '请输入货号', trigger: 'blur'},
        ],
        stock: [
          {required: true, message: '请输入库存', trigger: 'blur'},
        ],
        stockThreshold: [
          {required: true, message: '请输入库存阈警值', trigger: 'blur'},
        ],
        keywords: [
          {required: true, message: '请输入关键词', trigger: 'blur'},
        ],
        tags: [
          {required: true, message: '请输入标签', trigger: 'blur'},
        ],
        sort: [
          {required: true, message: '请输入排序序号', trigger: 'blur'},
        ],
      }
    };
  },
  methods: {
    backToPreStep() {
      // 返回上一步
      this.$router.push('/sys-admin/product/spu/add-new-1');
    },

    loadLocalRuleFrom() {
      // 从本地存储加载规则表单数据
      let localRuleFormString = localStorage.getItem('localRuleForm');
      if (localRuleFormString === null) {
        this.$router.push('/sys-admin/product/spu/add-new-1');
      }
      let localRuleForm = JSON.parse(localRuleFormString);
      if (localRuleForm.categoryId === null) {
        this.$router.push('/sys-admin/product/spu/add-new-1');
        console.log(localRuleForm);
      }
      this.ruleForm.categoryId = localRuleForm.categoryId;
      this.ruleForm.categoryName = localRuleForm.categoryName;
    },

    loadBrandList() {
      // 加载品牌列表
      let url = 'http://localhost:9080/brands';
      console.log('url = ' + url)
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .get(url).then((response) => {
        let responseBody = response.data;
        this.brandListOptions = responseBody.data;
        console.log(responseBody.data);
      });
    },

    submitForm(formName) {
      // 提交表单
      // 执行全部的检查规则
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // 请求检查选择的商品,判断是否存在
          let url = 'http://localhost:9080/brands/' + this.ruleForm.brandId;
          console.log('url = ' + url)
          this.axios
              .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
              .get(url).then((response) => {
            let responseBody = response.data;
            if (responseBody.state === 2000) {
              // 将所有数据存储到localStorage中
              this.ruleForm.brandName = responseBody.data.name;
              localStorage.setItem('localRuleFrom', JSON.stringify(this.ruleForm));
              let xxx = localStorage.getItem('localRuleFrom');
              let xxxObject = JSON.parse(xxx);
              console.log(xxxObject);
              console.log('即将跳转到第三步')
              // 跳转到下一个页面
              this.$router.push('/sys-admin/product/spu/add-new-3');
            } else {
              this.$message.error(responseBody.message);
            }
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },

    resetForm(formName) {
      // 重置表单
      this.$refs[formName].resetFields();
    }
  },

  mounted() {
    // 组件挂载时执行加载本地规则表单数据和加载品牌列表的方法
    this.loadLocalRuleFrom()
    this.loadBrandList();
  }
}
</script>
<style>

</style>